<template>
	<view>
		<!-- <nav-bar-sub></nav-bar-sub> -->
		<!-- 用于提示是否确认退出考试 -->
		<u-modal v-model="show" :content="content" :show-cancel-button="true" @confirm="handleConfirm"></u-modal>
		<!-- 渲染题目 -->
		<answer :isReviewed='isReview' :examType="examType" @submit='handleCommit' @timeEnd='handleConfirm'
			@onChange='onChange'></answer>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				content: '是否确认交卷',
				isReview: false,
				questionList: [],
				examType: 'exam', //考试类型
			}
		},
		onLoad(option) {
			this.examType = option.examType
			var that = this
			if (option.examType == 'exam') {
				// 理论考试
				that.$api.enterExam(that.$store.state.access_token, option.id)
					.then(res => {
						console.log(res.values);
						that.$store.state.examDetail = res.values
						var quesModules = res.values.modules
						// console.log(that.$store.state.examDetail);
						// 提取出所有的题目
						that.$api.ModifyQuesList(quesModules)
					})
			} else if (option.examType == 'paper') {
				// 模拟考试
				this.$api.paperStart(option.paperId)
					.then(res => {
						console.log('paperStart:', res);
						that.$store.state.examDetail = res.values
						console.log('paper examDetail:', res.values);
						var quesModules = res.values.modules
						that.$api.ModifyQuesList(quesModules)
						console.log(that.$store.state.pracDetail);
						console.log(that.$store.state.pracQuesList);
					})
			} else if (option.examType == 'know') {
				// 知识点练习
				const knowSearch = JSON.parse(decodeURIComponent(option.knowSearch));
				console.log(knowSearch);
				this.$api.startKnow(knowSearch)
					.then(res => {
						if (res.status == 1) {
							// 获取知识点失败
							this.$refs.uToast.show({
								title: res.message,
								type: 'error',
								// icon: false,
								url: '/pages/exam/pracIndexKnow'
							})
						} else {
							console.log('开始知识点练习的请求结果:', res);
							that.$store.state.examDetail = res.values
							var quesModules = res.values.modules
							that.$api.ModifyQuesList(quesModules)
							console.log(that.$store.state.pracDetail);
							console.log(that.$store.state.pracQuesList);
						}
					})
			}else if(option.examType == 'course'){
				this.$api.startTheory(that.$store.state.access_token, option.paperId)
				.then(res=>{
					that.$store.state.examDetail = res.values
					var quesModules = res.values.modules
					that.$api.ModifyQuesList(quesModules)
				})
			}else {
				console.log("考试类型错误，请输入正确的类型");
				// return false
			}

			console.log('考题列表:', this.questionList);
		},
		components: {
			Answer
		},
		methods: {
			/* 提交答案 显示再次确认*/
			handleCommit(questionList) {
				// 显示是否确认交卷
				this.show = true;
			},

			// 已确认进行答案提交
			handleConfirm() {
				// 进行交卷的api请求
				let that = this
				if (this.examType == 'exam') {
					this.$api.commitExam(this.$store.state.examDetail.exam.id, this.$store.state.examDetail.recordId)
						.then(res => {
							console.log('交卷:', res);
							that.$u.route('/pages/exam/examResultPage', {
								recordId: that.$store.state.examDetail.recordId,
								examType: 'exam'
							})
						})
				} else if ((this.examType == 'paper') || (this.examType == 'know')) {
					this.$api.commitPaper(this.$store.state.examDetail.exerciseId)
						.then(res => {
							console.log('交卷:', res);
							that.$u.route('/pages/exam/examResultPage', {
								exerciseId: that.$store.state.examDetail.exerciseId,
								examType: 'paper',
							})
						})
				} else if (this.examType == 'store') {
					this.$api.commitExam(this.$store.state.examDetail.exam.id, this.$store.state.examDetail.recordId)
						.then(res => {
							console.log('交卷:', res);
							that.$u.route('/pages/exam/examResultPage', {
								recordId: that.$store.state.examDetail.recordId,
								examType: 'exam'
							})
						})
				}else if(this.examType == 'course'){
					this.$api.commitCourse(this.$store.state.access_token, this.$store.state.examDetail.exerciseId)
					.then(res => {
						console.log('交卷:', res);
						that.$u.route('/pages/course/coursePracResult', {
							exerciseId: that.$store.state.examDetail.exerciseId,
							examType: 'course',
						})
					})
				}else {
					console.log("提交的考试类型出错");
				}
			},
			/* 题目答案变化
			 */
			onChange(answer) {
				console.log(answer)
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #FFFFFF;
	}
</style>